/**
 * Created by jack on 2017/12/14.
 */

'use strict';

goog.provide('Blockly.JavaScript.math');

goog.require('Blockly.JavaScript');

Blockly.JavaScript.math_whole_number = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};


Blockly.JavaScript.math_positive_number = function() {
    // Numeric value.
    var code = window.parseFloat(this.getFieldValue('NUM'));
    // -4.abs() returns -4 in Dart due to strange order of operation choices.
    // -4 is actually an operator and a number.  Reflect this in the order.
    var order = Blockly.JavaScript.ORDER_ATOMIC;
    return [code, order];
};
Blockly.JavaScript.math_angle = function() {
    // Numeric value.
    var code = window.parseFloat(this.getFieldValue('NUM'));
    // -4.abs() returns -4 in Dart due to strange order of operation choices.
    // -4 is actually an operator and a number.  Reflect this in the order.
    var order =  Blockly.JavaScript.ORDER_ATOMIC;
    return [code, order];
};

Blockly.JavaScript.math_number = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order =  Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.math_show_number = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'number_values',Blockly.JavaScript.ORDER_ATOMIC) || 0;
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order =  Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};


Blockly.JavaScript.operator_add = function() {
  var argument0 = Blockly.JavaScript.valueToCode(this, 'NUM1',Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.JavaScript.valueToCode(this, 'NUM2',Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var code = argument0+'+'+argument1;
  return [code,Blockly.JavaScript.ORDER_NONE];
};


Blockly.JavaScript.operator_subtract = function() {
  var argument0 = Blockly.JavaScript.valueToCode(this, 'NUM1',Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.JavaScript.valueToCode(this, 'NUM2',Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var code = argument0+'-'+argument1;
  return [code,Blockly.JavaScript.ORDER_NONE];
};


Blockly.JavaScript.operator_multiply = function() {
  var argument0 = Blockly.JavaScript.valueToCode(this, 'NUM1',Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.JavaScript.valueToCode(this, 'NUM2',Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var code = argument0+'*'+argument1;
  return [code,Blockly.JavaScript.ORDER_NONE];
};


Blockly.JavaScript.operator_divide = function() {
  var argument0 = Blockly.JavaScript.valueToCode(this, 'NUM1',Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.JavaScript.valueToCode(this, 'NUM2',Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var code = argument0+'/'+argument1;
  return [code,Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript.operator_mapping = function() {
  var value_map_value = Blockly.JavaScript.valueToCode(this, 'map_value', Blockly.JavaScript.ORDER_ATOMIC);
  var value_map_top = Blockly.JavaScript.valueToCode(this, 'map_top', Blockly.JavaScript.ORDER_ATOMIC);
  var value_map_bottom = Blockly.JavaScript.valueToCode(this, 'map_bottom', Blockly.JavaScript.ORDER_ATOMIC);
  var value_to_top = Blockly.JavaScript.valueToCode(this, 'to_top', Blockly.JavaScript.ORDER_ATOMIC);
  var value_to_bottom = Blockly.JavaScript.valueToCode(this, 'to_bottom', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var js_map_func='function map_func(value,map_top,map_bottom,to_top,to_bottom) {\nvar map_value=(value-map_top)*(to_bottom-to_top)/(map_bottom-map_top)+to_top;\nreturn map_value;\n}\n';
  Blockly.JavaScript.definitions_["js_map_func"]=js_map_func;
  var code ='map_func('+value_map_value+','+value_map_top+','+value_map_bottom+','+value_to_top+','+value_to_bottom+')';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript.operator_random = function() {
  var argument0 = Blockly.JavaScript.valueToCode(this, 'FROM',Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.JavaScript.valueToCode(this, 'TO',Blockly.JavaScript.ORDER_ATOMIC) || '0';

  //获取指定区间范围随机数，包括lowerValue和upperValue
  var randomFrom='function randomFrom(lowerValue,upperValue) {return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);}\n';
  Blockly.JavaScript.definitions_["js_randomFrom"]=randomFrom;
  var code = 'randomFrom('+argument0+','+ argument1+')';
  return [code,Blockly.JavaScript.ORDER_NONE];
};

